package(default_visibility = ["//visibility:public"])

load("@npm_angular_bazel//:index.bzl", "ng_module")
load("@build_bazel_rules_nodejs//:defs.bzl", "http_server", "rollup_bundle")
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
load("@npm_bazel_typescript//:index.bzl", "ts_devserver")

# Allow targets under sub-packages to reference the tsconfig.json file
exports_files(["tsconfig.json"])

ng_module(
    name = "src",
    srcs = glob(["*.ts"]),
    deps = [
        "//src/hello-world",
        "@npm//@angular/common",
        "@npm//@angular/core",
        "@npm//@angular/platform-browser",
        "@npm//@angular/router",
        "@npm//@types",
        "@npm//rxjs",
    ],
)

filegroup(
    name = "rxjs_umd_modules",
    srcs = [
        # do not sort
        "@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
        ":rxjs_shims.js",
    ],
)

ts_devserver(
    name = "devserver",
    entry_module = "bazel_integration_test/src/main",
    index_html = "index.html",
    scripts = [
        ":rxjs_umd_modules",
    ],
    serving_path = "/bundle.min.js",
    static_files = [
        "@npm//:node_modules/zone.js/dist/zone.min.js",
    ],
    deps = ["//src"],
)

rollup_bundle(
    name = "bundle",
    entry_point = ":main.ts",
    deps = [
        "//src",
        # TODO(kyliau): These are not necessary. Bundle compiles fine without
        # these deps.
        # "@npm//@angular/common",
        # "@npm//@angular/core",
        # "@npm//@angular/platform-browser",
        "@npm//@angular/router",  # needed here due to additional imports generated by ngc
        "@npm//rxjs",
    ],
)

web_package(
    name = "prodapp",
    assets = [
        # do not sort
        "@npm//:node_modules/zone.js/dist/zone.min.js",
        ":bundle.min.js",
    ],
    data = [
        ":bundle",
    ],
    index_html = "index.html",
)

http_server(
    name = "prodserver",
    data = [":prodapp"],
    templated_args = ["src/prodapp"],
)
